﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;


public partial class NewShipment : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindParcelType();
            bindCountry();
            bindoginedMemberInfo();
        }
    }

    private void bindCountry()
    {
        BLLShipment bll = new BLLShipment();
        ddlCountry.DataSource = bll.getCountries();
        ddlCountry.DataTextField = "Country";
        ddlCountry.DataValueField = "ID";
        ddlCountry.DataBind();

        ddlReCountry.DataSource = ddlCountry.DataSource;
        ddlReCountry.DataTextField = "Country";
        ddlReCountry.DataValueField = "ID";
        ddlReCountry.DataBind();
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //If it has not been an member
        //if (txtWeight.Text == "0")
        //{
        //    Page.ClientScript.RegisterStartupScript(typeof(Page), "Javascript", "alert('Weight cannot be 0');", true);
        //}
        //else
        //{

            if (!isLogin())
            {
                spanName.Text = txtName.Text;
                txtMemberEmail.Text = txtEmail.Text;
                ModalPopupExtender1.Show();
            }
            else
            {
                //Call Go on Button
                btnGoon_Click(sender, e);
            }
        }
    //}

    /// <summary>
    /// bind parcel type to ddlParcelType
    /// </summary>
    private void bindParcelType()
    {
        BLLShipment bll = new BLLShipment();
        ddlParcelType.DataSource = bll.getAllParcelType();
        ddlParcelType.DataTextField = "ParcelType";
        ddlParcelType.DataValueField = "ID";
        ddlParcelType.DataBind();
    }

    protected void btnGoon_Click(object sender, EventArgs e)
    {
        BLLShipment bllShipment = new BLLShipment();
        tblCustomer senderCustomer = null;
        if (isLogin())
            senderCustomer = LoginedMember;

        //If it is not a member
        if (senderCustomer == null)
        {
            //New Sender Customer
            senderCustomer = new tblCustomer()
            {
                Name = txtName.Text,
                NRIC = txtNRIC.Text,
                Block = txtBlock.Text,
                Street = txtStreet.Text,
                City = txtCity.Text,
                Province = txtProvince.Text,
                Country = Convert.ToInt16(ddlCountry.SelectedValue),
                Contact = txtContact.Text,
                Email = txtEmail.Text,
            };
            bllShipment.NewCustomer(senderCustomer);
        }

        //New Receiver Customer
        tblCustomer receiverCustomer = new tblCustomer()
        {
            Name = txtReName.Text,
            Contact = txtReContact.Text,
            Block = txtReBlock.Text,
            Street = txtReStreet.Text,
            City = txtReCity.Text,
            Province = txtReProvince.Text,
            Country = Convert.ToInt16(ddlReCountry.SelectedValue),
            Email = "",
            IsMember = false
        };
        bllShipment.NewCustomer(receiverCustomer);

        //New Package
        tblPackage package = new tblPackage()
        {
            ParcelType = Convert.ToInt16(ddlParcelType.SelectedValue),
            Fragile = Convert.ToBoolean(ddlFragile.SelectedValue),
            Weight = Convert.ToDecimal(txtWeight.Text),
            Sender = senderCustomer.ID,
            Receiver = receiverCustomer.ID,
            Status = 1,
            CreatedDate = DateTime.Now
        };

        bllShipment.NewPackage(package);

        PrivateFontCollection p = new PrivateFontCollection();
        p.AddFontFile(Server.MapPath("~/Styles/fre3of9x.ttf"));
        System.Drawing.Font oFont = new System.Drawing.Font(p.Families[0], 50);
        bllShipment.GenerateReceipt(package.ID, Server.MapPath("~/Receipt/" + package.Barcode + ".pdf"), oFont);
        string script = "window.open('Receipt.aspx?barcode=" + package.Barcode + "','_blank');window.location.reload();";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "newopen", script, true);
        
        //Response.Redirect("~/Receipt.aspx?barcode=" + package.Barcode);
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        BLLLogin bllLogin = new BLLLogin();
        tblCustomer member = bllLogin.login(txtLoginUserName.Text, txtLoginPassword.Text);
        if (member != null)
        {
            LoginedMember = member;
        }

        if (!isLogin())
        {
            panelLogin.Style["display"] = "block";
            lblLoginFail.Visible = true;
            return;
        }

        bindoginedMemberInfo();
        linkNonMember.Visible = false;
    }

    private void bindoginedMemberInfo()
    {
        if (isLogin())
        {
            linkNonMember.Visible = false;
            txtName.Text = LoginedMember.Name;
            txtEmail.Text = LoginedMember.Email;
            txtNRIC.Text = LoginedMember.NRIC;
            txtBlock.Text = LoginedMember.Block;
            txtStreet.Text = LoginedMember.Street;
            txtCity.Text = LoginedMember.City;
            txtProvince.Text = LoginedMember.Province;
            ddlCountry.SelectedValue = LoginedMember.Country.HasValue ? LoginedMember.Country.Value.ToString() : null;
            txtContact.Text = LoginedMember.Contact;
        }
    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (txtMemberPassword.Text != txtMemberRePassword.Text)
        {
            lblPasswordError.Text = "Password does not match";
            lblPasswordError.Visible = true;
            ModalPopupExtender1.Show();
            return;
        }
        else if (string.IsNullOrEmpty(txtMemberEmail.Text))
        {
            lblPasswordError.Text = "Email Invalid";
            lblPasswordError.Visible = true;
            ModalPopupExtender1.Show();
            return;
        }

        try
        {
            BLLRegistration bllRegistration = new BLLRegistration();
            if (bllRegistration.CheckEmailExist(txtEmail.Text))
            {
                lblEmailExist.Visible = true;
                ModalPopupExtender1.Show();
                return;
            }

            tblCustomer customer = new tblCustomer()
            {
                Name = txtName.Text,
                NRIC = txtNRIC.Text,
                Block = txtBlock.Text,
                Street = txtStreet.Text,
                City = txtCity.Text,
                Province = txtProvince.Text,
                Country = Convert.ToInt16(ddlCountry.SelectedValue),
                Contact = txtContact.Text,
                Email = txtMemberEmail.Text,
                Password = new Hash().hash(txtMemberPassword.Text),
                IsMember = true
            };

            //BLLRegistration bllRegistration = new BLLRegistration();
            bllRegistration.register(customer);
            try
            {
                bllRegistration.sendEmail(customer.Email);
            }
            catch { ;}
            LoginedMember = customer;
            btnGoon_Click(sender, e);
        }
        catch
        {

        }
    }
}